const {
    arrayToMap
} = rx
const app = new Vue({
    el: '#app',
    data: {
        tlogSecurityVo: new TlogSecurity(),
        page: {},
        entity: new TlogSecurityVo(),
        records: [],
        startIndex: 0,
        endIndex: 0,
        nowIndex: 0,
        currentPageStartIndex: 0,
        indexAndIdMap: new Map(),
        sysRoleList: [],
        sysRoleMap: new Map(),
    },
    methods: {
        initAttrStr: function () {
            this.tlogSecurityVo.eventTypeStr = fieldFormatter.generator(fieldFormatter.tlogSecurity.eventTypeMap)(this.tlogSecurityVo.eventType)
            this.tlogSecurityVo.userTypeStr = fieldFormatter.generator(fieldFormatter.tlogSecurity.userTypeMap)(this.tlogSecurityVo.userType)
            this.tlogSecurityVo.actTimeStr = !this.tlogSecurityVo.actTime ? '' : rx.dateFormat(new Date(this.tlogSecurityVo.actTime), 'yyyy-MM-dd hh:mm:ss')
        },
        async tlogSecurityInfo() {
            this.tlogSecurityVo = await tlogSecurityApi.get(forward.getData().id)
            this.initAttrStr()
            this.page = forward.getData().page
            this.currentPageStartIndex = forward.getData().pagestartIndex
            this.endIndex = forward.getData().endIndex
            this.entity = forward.getData().entity
            const id = forward.getData().id
            this.records = this.page.records
            for (let i = 0; i < this.records.length; i++) {
                const nowid = this.records[i].id
                this.indexAndIdMap.set(this.currentPageStartIndex + i, nowid)
                if (id === nowid) {
                    this.nowIndex = this.currentPageStartIndex + i
                }
            }

        },
        //返回列表
        returnSuper() {
            forward('security-log/admin_security_securityloglist', {
                page: this.page,
                entity: this.entity,
                isfromSub: true,
            })
        },
        //上一页n
        async previousPage() {
            if (this.nowIndex === 0) {
                rxPrompt.msg('这是第一条数据，没有上一条啦')
                return
            }
            if (this.nowIndex === this.currentPageStartIndex) {
                //获取记录表上页的数据
                this.page.current--
                this.page = await tlogSecurityApi.page(this.page, this.entity)
                this.records = this.page.records
                this.currentPageStartIndex = this.currentPageStartIndex - this.page.size
                for (let i = 0; i < this.records.length; i++) {
                    const nowid = this.records[i].id
                    this.indexAndIdMap.set(this.currentPageStartIndex + i, nowid)
                }

            }
            this.nowIndex--
            this.tlogSecurityVo = await tlogSecurityApi.get(this.indexAndIdMap.get(this.nowIndex))
            this.initAttrStr()

        },
        //下一页
        async nextPage() {
            if (this.nowIndex === this.endIndex) {
                rxPrompt.msg('这是第最后一条数据，没有下一条啦')
                return
            }
            if (this.nowIndex === this.currentPageStartIndex + this.page.size - 1) {
                //获取记录表上页的数据
                this.page.current++
                this.page = await tlogSecurityApi.page(this.page, this.entity)
                this.records = this.page.records
                this.currentPageStartIndex = this.currentPageStartIndex + this.page.size
                for (let i = 0; i < this.records.length; i++) {
                    const nowid = this.records[i].id
                    this.indexAndIdMap.set(this.currentPageStartIndex + i, nowid)
                }

            }
            this.nowIndex++
            this.tlogSecurityVo = await tlogSecurityApi.get(this.indexAndIdMap.get(this.nowIndex))
            this.initAttrStr()

        },
        async sysRoleListInit() {
            this.sysRoleList = await sysRoleApi.listForAll();
            this.sysRoleMap = arrayToMap(this.sysRoleList, ({id}) => id, ({name}) => name)
        },
    },
    computed: {
        fieldFormatter() {
            return fieldFormatter
        },
    },
    mounted() {
        this.sysRoleListInit();
        this.tlogSecurityInfo()
    },

})